package com.fanfan.SmartPensionSystembackend.controller;

import com.fanfan.SmartPensionSystembackend.entity.CheckInfo;
import com.fanfan.SmartPensionSystembackend.entity.Elder;
import com.fanfan.SmartPensionSystembackend.service.ElderService;
import com.fanfan.SmartPensionSystembackend.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/elder")
@CrossOrigin
public class ElderController {

    @Autowired
    private ElderService elderService;

    // 增加用户
    @PostMapping("/register")
    public Map<String, Object> checkInElder(@RequestBody CheckInfo checkInfo) {
        try {
            System.out.println("Received elder: " + checkInfo);
            // 调用 Service 层完成注册逻辑
            String isSuccess = elderService.checkInElder(checkInfo);
            if (isSuccess!=null) {
                return ResponseUtil.success("入住成功",isSuccess);
            } else {
                return ResponseUtil.error("入住失败，请稍后重试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error("系统异常：" + e.getMessage());
        }
    }

    // 根据传来的id查询用户
    @GetMapping("/getElderInfo")
    public Map<String, Object> getElder(@RequestParam  String elderId) {
        try {
            System.out.println("Received elderId: " + elderId);
            Elder elder = elderService.getElderWithDetails(elderId);
            if (elder != null) {
                return ResponseUtil.success("查询成功", elder);
            } else {
                return ResponseUtil.error("查询失败，用户不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error("系统异常：" + e.getMessage());
        }
    }

    // 修改用户信息
    @PostMapping("/updateElderInfo")
    public Map<String, Object> updateElder(@RequestBody CheckInfo checkInfo) {
        try {
            System.out.println("收到的更新内容: " + checkInfo.getCheckInfo());
            boolean isSuccess = elderService.updateElderInfo(checkInfo);
            if (isSuccess != false) {
                return ResponseUtil.success("修改成功", null);
            } else {
                return ResponseUtil.error("修改失败，用户不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error("系统异常：" + e.getMessage());
        }
    }

    // 根据id删除用户
    @DeleteMapping("/deleteElderInfo")
    public Map<String, Object> deleteElder(@RequestParam String elderId) {
        try {
            System.out.println("Received elderId: " + elderId);
            boolean isSuccess = elderService.deleteElderInfo(elderId);
            if (isSuccess) {
                return ResponseUtil.success("删除成功", null);
            } else {
                return ResponseUtil.error("删除失败，用户不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error("系统异常：" + e.getMessage());
        }
    }
}